/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package makao;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import shared.Card;

/**
 *
 * @author gekon
 */
public class TablePanel extends JPanel
{

    private MakaoView makaoView = null;
    private Boolean paintableFlag = false;
    private ArrayList<Card> tableCards = new ArrayList<Card>();
    private ArrayList<Point> tableCardsTransformRatio = new ArrayList<Point>();
    private ArrayList<Integer> tableCardsRotateRatio = new ArrayList<Integer>();
    private Graphics2D graph2D;
    private ImageIcon tableIcon;
    private Image tableImage;
    private Random rand = new Random();
    private PlayerData userData;
    private Boolean takeCardFlag = false;
    private Boolean waitFlag = false;
    private int takeCardFlagCounter = 5;
    private int waitFlagCounter = 5;
    private AffineTransform identity = new AffineTransform();
    private AffineTransform trans = new AffineTransform();

    @Override
    public void paint(Graphics g)
    {
        graph2D = (Graphics2D) g;
        super.paintComponent(graph2D);

        if (paintableFlag)
        {
            setBackground();
            graph2D.drawImage(tableImage, 0, 0, this.getWidth(), this.getHeight(), this);

            try
            {
                for (int i = 0; i < tableCards.size(); i++)
                {
                    identity.setToTranslation(this.getWidth() / 2 - 70 + tableCardsTransformRatio.get(i).x,
                            this.getHeight() / 2 - 105 + tableCardsTransformRatio.get(i).y);
                    trans.setTransform(identity);
                    trans.rotate(Math.toRadians(tableCardsRotateRatio.get(i)));

                    graph2D.drawImage((new ImageIcon("src/images/oryginal/" + getImagePackage(tableCards.get(i).getSuit())
                            + "/" + getImageFileName(tableCards.get(i).getType()) + ".png")).getImage(), trans, this);
                }

                graph2D.drawImage((new ImageIcon("src/images/extra/cardBack.png")).getImage(),
                        this.getWidth() - 80, this.getHeight() - 105, this);
                graph2D.drawImage((new ImageIcon("src/images/extra/waitIcon.png")).getImage(),
                        this.getWidth() - 80, this.getHeight() - 50, this);

                if (takeCardFlag == true)
                {
                    takeCardFlagCounter--;
                    graph2D.drawImage((new ImageIcon("src/images/extra/cardBackLight.png")).getImage(),
                            this.getWidth() - 80, this.getHeight() - 105, this);

                    if (takeCardFlagCounter == 0)
                    {
                        takeCardFlag = false;
                        takeCardFlagCounter = 5;
                    }
                }

                if (waitFlag == true)
                {
                    waitFlagCounter--;
                    graph2D.drawImage((new ImageIcon("src/images/extra/waitIconClick.png")).getImage(),
                            this.getWidth() - 80, this.getHeight() - 50, this);

                    if (waitFlagCounter == 0)
                    {
                        waitFlag = false;
                        waitFlagCounter = 5;
                    }
                }
            } catch (Exception e)
            {
            }
        }
    }

    public void setBackground()
    {
        tableIcon = new ImageIcon("src/images/table.png");
        tableImage = tableIcon.getImage();

        this.repaint();
    }

    public void setTable(PlayerData data, MakaoView makaoView)
    {
        this.makaoView = makaoView;
        this.userData = data;
        paintableFlag = true;
    }

    public void checkTableClick(int x, int y)
    {
        if (this.isEnabled())
        {
            if ((x > (this.getWidth() - 80)) && (x < (this.getWidth() - 30)) && (y > (this.getHeight() - 105)) && (y < (this.getHeight() - 55)))
            {
                takeCardFlag = true;
                makaoView.getPlayerAction().takeCard();
            }

            if ((x > (this.getWidth() - 80)) && (x < (this.getWidth() - 30)) && (y > (this.getHeight() - 50)))
            {
                waitFlag = true;
                makaoView.getPlayerAction().waitTurns();
            }
        }
    }

    public void layCard(Card card)
    {
        //for (Card card : cards)
        //{
        tableCards.add(new Card(card.getSuit(), card.getType()));
        rand.setSeed(new Date().getTime());
        tableCardsRotateRatio.add(rand.nextInt(90) - 45);
        tableCardsTransformRatio.add(new Point(rand.nextInt(40) - 20, rand.nextInt(80) - 40));
        //}
    }

    public void clearTable()
    {
        tableCardsRotateRatio.clear();
        tableCardsTransformRatio.clear();
        tableCards.clear();
        this.repaint();
    }

    private String getImagePackage(int cardSuit)
    {
        switch (cardSuit)
        {
            case Card.NONE_SUIT:
                return "extra";
            case Card.HEART:
                return "heart";
            case Card.DIAMOND:
                return "diamond";
            case Card.CLUB:
                return "club";
            case Card.SPADE:
                return "spade";
        }
        return null;
    }

    private String getImageFileName(int cardType)
    {
        if (cardType <= Card.TEN)
        {
            return Integer.toString(cardType);
        } else
        {
            switch (cardType)
            {
                case Card.ACE:
                    return "A";
                case Card.KING:
                    return "K";
                case Card.QUEEN:
                    return "Q";
                case Card.JACK:
                    return "J";
                case Card.JOKER:
                    return "jocker";
            }
        }
        return null;
    }

    public void setPaintableFlag(Boolean value)
    {
        this.paintableFlag = value;
    }
}
